﻿if (typeof Phule == "undefined") Phule = {};
if (typeof Phule.Web == "undefined") Phule.Web = {};
Phule.Web.Contact = {
    Init: function () {
        var objThis = this;

        $("#btnsent").bind("click", function () {
            if ($(this).hasClass('disabled'))
                return;
            objThis.SendMessage();
        });
        $("#txtName, #txtPhone, #txtMessage,#txtEmail").bind('keyup', function () {
            if (objThis.CheckValidate()) {
                $("#btnsent").removeAttr('disabled');
                $("#btnsent").removeClass('disabled');
            }
            else {
                $("#btnsent").attr('disabled', 'disabled');
                $("#btnsent").addClass('disabled');
            }
        });
        $("#txtPhone").keypress(function (e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                //display error message
                $("#errmsg").html("Digits Only").show().fadeOut("slow");
                return false;
            }
        });


    },
    SendMessage: function () {
        var name = $.trim($("#txtName").val());
        var email = $.trim($("#txtEmail").val());
        var phone = $.trim($("#txtPhone").val());
        var message = $.trim($("#txtMessage").val());
        var pData = { Name: name, Email: email, Message: message, Phone: phone };
        $.ajax({
            url: '../services/ContactService.asmx/AddContactMessage',
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            type: 'POST',
            data: JSON.stringify(pData),
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    $('#myModal').modal({});
                    $("#txtName, #txtPhone, #txtMessage,#txtEmail").val('');
                    $("#btnsent").attr('disabled', 'disabled');
                    $("#btnsent").addClass('disabled');
                }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    },
    CheckValidate: function () {
        var objThis = this;
        var name = $.trim($("#txtName").val());
        var email = $.trim($("#txtEmail").val());
        var phone = $.trim($("#txtPhone").val());
        var message = $.trim($("#txtMessage").val());      
        if (name == "" || message == "")
            return false;
        if (phone == "" && email == "")
            return false;
        if (email != "")
            return objThis.ValidateEmail(email);
        return true;

    },
    ValidateEmail: function (email) {
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(email);
    }

}
$(function () {
    Phule.Web.Contact.Init();
});